/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-14 11:03:23
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-26 14:33:08
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/new_air_cooling_page/new_air_cooling_page_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import 'package:yt_devops/pages/devices/device_tab/components/detail_grid_widget.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/my_top_tabbar.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'new_air_cooling_page_controller.dart';

class NewAirCoolingPage extends GetView<NewAirCoolingPageController> {
  final bool isNew; //是否为新风冷和液冷
  final DeviceModel? deviceModel;
  final List<int> subDevicesIdList; //设备详情ems需要订阅的子设备
  final List<String> subDevicesNameList; //设备详情ems需要订阅的子设备名称
  DeviceListModel? deviceTree;
  NewAirCoolingPage(
      {this.isNew = true,
      this.deviceModel,
      this.subDevicesIdList = const [],
      this.subDevicesNameList = const [],
      this.deviceTree,
      Key? key})
      : super(key: key);
  @override
  // NewAirCoolingPageController get controller =>
  //     Get.find();
  final actionKey = GlobalKey();

  @override
  NewAirCoolingPageController controller =
      Get.put<NewAirCoolingPageController>(NewAirCoolingPageController());

  @override
  Widget build(BuildContext context) {
    // Get.put(NewAirCoolingPageController());
    return GetBuilder<NewAirCoolingPageController>(
        init: NewAirCoolingPageController(),
        initState: (state) {
          Log.d('initState===' + controller.toString());
          controller.deviceModel = deviceModel;
          controller.isNew = isNew;
          controller.subDevicesIdList = subDevicesIdList;
          controller.subDevicesNameList = subDevicesNameList;
          controller.deviceTree = deviceTree;
          if (deviceModel != null) {
            if (isNew) {
              if (controller.isInit == false) {
                //防止多次请求数据
                // Log.d('showMap===' + controller.showMap.toString());
                //新风液冷
                controller.initData(deviceModel!);
              } else {}
            } else {
              controller.init(deviceModel!);
            }
          }
        },
        dispose: (state) {
          // Get.delete<NewAirCoolingPageController>();
        },
        builder: (_) => Scaffold(
              backgroundColor: Colour.backgroundColor,
              appBar: controller.actionList.isEmpty
                  ? AppBarWidget.defaultAppBar(
                      controller.deviceModel?.name ?? '')
                  : AppBarWidget.withMorePopMenu(
                      controller.deviceModel?.name ?? '',
                      context,
                      actionKey,
                      data: controller.actionList,
                      onItemClick: (int index, dynamic value) async {
                        await controller.toService(value);
                        return true;
                      },
                    ),
              body: controller.isNew!
                  ? Container(
                      color: controller.tabs.isNotEmpty
                          ? Colors.white
                          : Colour.backgroundColor,
                      child: (controller.tabs.isEmpty && controller.isLoading)
                          ? Container(
                              // color: Colors.red,
                              width: ScreenUtil().screenWidth,
                              child: ListEmptyWidget(topPosition: 82),
                            )
                          : Column(
                              children: [
                                Container(
                                  color: Colors.white,
                                  padding:
                                      const EdgeInsets.only(left: 6, right: 6),
                                  child: MyTopTabBar(controller.tabController!,
                                      controller.tabs),
                                ),
                                Container(
                                    height: 10, color: Colour.backgroundColor),
                                Expanded(
                                  child: Container(
                                    color: Colour.backgroundColor,
                                    child: TabBarView(
                                        controller: controller.tabController,
                                        children: controller.tabviewList
                                            .map((tabview) {
                                          return KeepAliveWrapper(tabview);
                                        }).toList()),
                                  ),
                                ),
                                Container(
                                    height: 10, color: Colour.backgroundColor),
                              ],
                            ),
                    )
                  : StreamBuilder(
                      stream: controller.streamController?.stream,
                      builder: (context, snapshot) {
                        var dataSource = snapshot.data?.map((key, value) =>
                                MapEntry(
                                    key.replaceAll(
                                        '-${deviceModel?.deviceId}', ''),
                                    value)) ??
                            {};

                        return SmartRefresher(
                          controller: RefreshController(),
                          enablePullDown: false,
                          enablePullUp: false,
                          child: ListView.builder(
                            padding: EdgeInsets.zero,
                            itemBuilder: (context, index) =>
                                DeviceUtil.blockContainer(
                              Column(
                                children: [
                                  DetailGridWidget(
                                    isSingleRow: controller.isSingleLine,
                                    title:
                                        controller.modelList[index].groupName ??
                                            '',
                                    items: controller.modelList[index].items,
                                    dataSource: dataSource,
                                    isLightTtext: controller.connectStatus !=
                                        ConnectStatusType.on,
                                  ),
                                ],
                              ),
                            ),
                            itemCount: controller.modelList.length,
                          ),
                        );
                      }),
            ));
  }
}
